package data.framework.web.widget;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class HeaderTag extends TagSupport {

	private static final long serialVersionUID = -4102881820406942327L;

	private String title = "";
	private String charset = "";
	private String script = "";
	private String style = "";
	private String noCache = "";
	private String debug = "";

	public int doStartTag() throws JspException {
		if ("".equals(title)) {
			title = "企业通用开发平台";
		}
		if ("".equals(charset)) {
			charset = "utf-8";
		}
		if ("".equals(noCache)) {
			noCache = "true";
		}
		if ("".equals(debug)) {
			debug = "false";
		}
		String ctx = pageContext.getServletContext().getContextPath();

		try {
			pageContext.getOut().write("<title>" + title + "</title>");
			pageContext.getOut().write("\r\n");

			pageContext.getOut().write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=" + charset + "\"/>");
			pageContext.getOut().write("\r\n");

			if ("true".equals(noCache)) {
				pageContext.getOut().write("<meta http-equiv=\"Cache-Control\" content=\"no-cache\"/>");
				pageContext.getOut().write("\r\n");
				pageContext.getOut().write("<meta http-equiv=\"Pragma\" content=\"no-cache\"/>");
				pageContext.getOut().write("\r\n");
				pageContext.getOut().write("<meta http-equiv=\"Expires\" content=\"0\"/>");
			}

			pageContext.getOut().write("\r\n");

			if ("true".equals(debug)) {
				pageContext.getOut().write("<script src=\"" + ctx + "/script/firebug/firebug-lite.js\" type=\"text/javascript\"></script>");
				pageContext.getOut().write("\r\n");
			}

			pageContext.getOut().write("\r\n");

			if (!"".equals(script)) {
				String[] scripts = script.split(",");
				for (String js : scripts) {
					pageContext.getOut().write("<script src=\"" + ctx + "/" + js + "\" type=\"text/javascript\"></script>");
					pageContext.getOut().write("\r\n");
				}
			}

			pageContext.getOut().write("\r\n");

			if (!"".equals(style)) {
				String[] styles = style.split(",");
				for (String css : styles) {
					pageContext.getOut().write("<link href=\"" + ctx + "/" + css + "\" type=\"text/css\" rel=\"stylesheet\" />");
					pageContext.getOut().write("\r\n");
				}
			}

			pageContext.getOut().write("\r\n");

			pageContext.getOut().write("<script src=\"" + ctx + "/script/jquery/jquery.js\" type=\"text/javascript\"></script>");
			pageContext.getOut().write("\r\n");

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				pageContext.getOut().flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return EVAL_PAGE;

	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		this.script = script;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getNoCache() {
		return noCache;
	}

	public void setNoCache(String noCache) {
		this.noCache = noCache;
	}

	public String getDebug() {
		return debug;
	}

	public void setDebug(String debug) {
		this.debug = debug;
	}

}
